/*
*    l3qd - Light, light, lightweight queue
*    Copyright (C) 2024  Marcus Pedersén marcus@marcux.org
*
*    This program is free software: you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation, either version 3 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License
*    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package set

import (
	"fmt"
	"github.com/spf13/cobra"
	"notabug.org/marcux/l3q/internal/client"
	"os"
	"os/user"
	"strconv"
)

var setNodeCmdFlagVerbose, setNodeCmdFlagOnline, setNodeCmdFlagOffline bool

var setNodeCmd = &cobra.Command{
	Use:   "node NODEID",
	Args:  cobra.ExactArgs(1),
	Short: "Set state of node to online or offline.",
	Long: `Description:
  Set state of node to online or offline.
  NODEID is required as a positional argument and is the id number of node to set state on.
  User is required to be root to change state.
  Only either --online or --offline flag is required`,
	Run: func(cmd *cobra.Command, args []string) {
		u, err := user.Current()
		if err != nil {
			if setNodeCmdFlagVerbose {
				fmt.Fprintf(os.Stderr, "Failed to get current user.\n%s\n", err)
			} else {
				fmt.Fprintf(os.Stderr, "Failed to get current user.\n")
			}

			os.Exit(1)
		}

		if u.Uid != "0" || u.Username != "root" {
			fmt.Fprintf(os.Stderr, "Permission denied.\n")
			os.Exit(1)
		}

		nodeid, err := strconv.Atoi(args[0])

		if err != nil {
			fmt.Fprintf(os.Stderr, "Failed to parse NODEID: %s, as integer.\nNODEID is required to be a positive integer.\nTry -h or --help for help.\n",
				args[0])

			if setNodeCmdFlagVerbose {
				fmt.Fprintf(os.Stderr, "%s\n", err)
			}

			os.Exit(1)
		}

		if nodeid <= 0 {
			fmt.Fprintf(os.Stderr, "NODEID is required to be a positive integer.\nTry -h or --help for help.\n")
			os.Exit(1)
		}

		if setNodeCmdFlagOnline && setNodeCmdFlagOffline {
			fmt.Fprintf(os.Stderr, "Only one of flags --online and --offline can be supplied.\nTry -h or --help for help.\n")
			os.Exit(1)
		} else if !setNodeCmdFlagOnline && !setNodeCmdFlagOffline {
			fmt.Fprintf(os.Stderr, "Flag --online or --offline is required.\nTry -h or --help for help.\n")
			os.Exit(1)
		}

		setNode(nodeid)
	},
}

func setNode(nodeid int) {
	urlSuffix := fmt.Sprintf("/node/state?id=%d", nodeid)

	if setNodeCmdFlagOnline {
		urlSuffix = fmt.Sprintf("%s&online=true", urlSuffix)
	} else {
		urlSuffix = fmt.Sprintf("%s&online=false", urlSuffix)
	}

	client.UrlPatch(urlSuffix, setNodeCmdFlagVerbose)

	if setNodeCmdFlagOnline {
		fmt.Printf("Node with id: %d successfully set online.\n", nodeid)
	} else {
		fmt.Printf("Node with id: %d successfully set offline.\n", nodeid)
	}
}

func init() {
	setNodeCmd.Flags().BoolVarP(&setNodeCmdFlagVerbose, "verbose", "v", false, "Print verbose output")
	setNodeCmd.Flags().BoolVar(&setNodeCmdFlagOnline, "online", false, "Set node online.")
	setNodeCmd.Flags().BoolVar(&setNodeCmdFlagOffline, "offline", false, "Set node offline.")
}
